<?php

class BannersController extends Controller
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $layout='//layouts/admin/main';

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('login','logout'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update', 'index', 'view', 'new', 'edit'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}

	public function actionNew()
	{
		$banner = new Banners;
		$this->render('new',array(
			'banner' => $banner,
		));
	}

	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}

	public function actionCreate()
	{
		if($_POST['Banners'])
		{
      $data = $_POST['Banners'];
			$model=new Banners('create');
			$model->attributes=$_POST['Banners'];
      $model->image=CUploadedFile::getInstance($model,'upload');
      $model->modified_at = date('Y-m-d H:i:s');

			if($model->validate('create'))
			{
        $model->save();
        $model->image->saveAs(Yii::app()->basePath. '/../assets/banners/' . $model->image);

        Yii::app()->user->setFlash('success', 'Success');
        Yii::app()->user->setFlash('content', 'You have successfully add new banner.');

				$this->redirect(array('index'));
			}
			else
			{
				$members = Members::model()->findAll();

				$this->render('new',array(
					'news'=>$model,
					'members'=>$members,
				));

			}
		}
	}

	public function actionEdit()
	{
		$banner = Banners::model()->find('id = :id',array(':id' => $_GET['id']));
		$this->render('edit',array(
			'banner'=>$banner,
		));
	}

	public function actionUpdate()
	{
		if(isset($_POST['Banners']))
		{
			$data=$_POST['Banners'];
      $model = new Banners('update');
			$banner=Banners::model()->find('id = :id',array(':id' => $data['id']));
			$banner->attributes=$data;
      $banner->modified_at = date('Y-m-d H:i:s');

      $tmp=CUploadedFile::getInstance($model,'upload');

      if(isset($tmp))
      {
        // delete existing image in storage
        unlink('assets/banners/'.$banner->image);

        $banner->image=CUploadedFile::getInstance($model,'upload');
      }

			if($banner->validate('update'))
			{
       if(isset($tmp))
        {
          Yii::app()->user->setFlash('success', 'Success');
          Yii::app()->user->setFlash('content', 'You have updated your banner.');
          $banner->image->saveAs(Yii::app()->basePath. '/../assets/banners/' . $banner->image);
        }
        $banner->save();
				$this->redirect(array('index'));
			}

      Yii::app()->user->setFlash('error', 'Error');
      Yii::app()->user->setFlash('content', 'Something went wrong. Please try again later.');
			$this->redirect(array('index'));

		}
	}

	public function actionDelete()
	{
		$banner=Banners::model()->find('id = :id', array(':id'=>$_GET['id']));
		$banner->delete();
		$this->redirect(bu('admin/banners/'));
	}

	public function actionIndex()
	{
		$banners=Banners::model()->findAll();
		$this->render('index',array(
			'banners' => $banners,
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Banners::model()->findByPk((int)$id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='banners-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
